﻿using System;
using System.Collections.Generic;
using System.Text;

namespace YArchitech.MEP
{
	public class HYBranch : List<HYComponent>
	{
		public int Index { get; set; }

		public string ShowName
		{
			get
			{
				return "分支" + this.Index.ToString();
			}
		}

		public HYComponent Terminal
		{
			get
			{
				if (base.Count == 0)
				{
					return null;
				}
				return base[base.Count - 1];
			}
		}

		public double Length
		{
			get
			{
				return this.m_Length;
			}
		}

		public double Resistance
		{
			get
			{
				return this.m_Resistance;
			}
		}

		public double UnbalanceRate
		{
			get
			{
				return this.m_UnbalanceRate;
			}
		}

		public string GetShowPathString()
		{
			StringBuilder stringBuilder = new StringBuilder();
			foreach (HYComponent hycomponent in this)
			{
				if (hycomponent is HYTube)
				{
					stringBuilder.Append((hycomponent as HYTube).ShowString);
					stringBuilder.Append(this.Connect);
				}
			}
			return stringBuilder.ToString().TrimEnd(this.Connect.ToCharArray());
		}

		public string GetShowIndexString()
		{
			StringBuilder stringBuilder = new StringBuilder();
			foreach (HYComponent hycomponent in this)
			{
				if (hycomponent is HYTube)
				{
					stringBuilder.Append(hycomponent.Index);
					stringBuilder.Append(this.Connect);
				}
			}
			return stringBuilder.ToString().TrimEnd(this.Connect.ToCharArray());
		}

		public virtual void AddFirst(HYComponent comp)
		{
			base.Insert(0, comp);
			if (comp is HYTube)
			{
				HYTube hytube = comp as HYTube;
				this.m_Length += hytube.Length;
				this.m_Resistance += hytube.Resistance;
			}
		}

		public virtual HYTube GetLastTube()
		{
			HYTube result;
			try
			{
				HYComponent hycomponent = base.FindLast((HYComponent comp) => comp is HYTube);
				result = (hycomponent as HYTube);
			}
			catch
			{
				result = null;
			}
			return result;
		}

		public virtual void ResetPipelineIndex(ref int index)
		{
			foreach (HYComponent hycomponent in this)
			{
				if (hycomponent is HYTube)
				{
					hycomponent.Index = index;
					index++;
				}
			}
		}

		public double CalUnbalanceRate(double referenceResistance)
		{
			this.m_UnbalanceRate = 1.0 - this.Resistance / referenceResistance;
			return this.m_UnbalanceRate;
		}

		public virtual void Cal()
		{
			this.m_Length = 0.0;
			this.m_Resistance = 0.0;
			foreach (HYComponent hycomponent in this)
			{
				if (hycomponent is HYTube)
				{
					HYTube hytube = hycomponent as HYTube;
					this.m_Length += hytube.Length;
					this.m_Resistance += hytube.Resistance;
				}
			}
		}

		public static HYBranch BuildBranch(HYComponent terminal)
		{
			HYBranch hybranch = new HYBranch();
			hybranch.AddFirst(terminal);
			for (HYComponent previous = terminal.Previous; previous != null; previous = previous.Previous)
			{
				hybranch.AddFirst(previous);
			}
			return hybranch;
		}

		protected double m_Resistance;

		protected double m_UnbalanceRate;

		protected double m_Length;

		private string Connect = "-";
	}
}
